/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.luxoft.corporate.blog.db;

/**
 *
 * @author Egor
 */
public class QueryImpl implements Query {

    private StringBuilder requestBuilder;
    private static final String SPACE_CHARACTER = " ";
    private static final String UP_COMMA = "\"";

    private QueryImpl(SupportedRequestTypes requestType) {
        requestBuilder = new StringBuilder(requestType.getSqlRepresentation());
    }

    @Override
    public void addBooleanParametr(Boolean boolParam) {
        requestBuilder.append(boolParam);
    }

    @Override
    public void addSqlCommand(String sqlCommand) {
        requestBuilder.append(SPACE_CHARACTER);
        requestBuilder.append(sqlCommand);
        requestBuilder.append(SPACE_CHARACTER);
    }

    @Override
    public void addStringParametr(String query) {
        requestBuilder.append(SPACE_CHARACTER);
        requestBuilder.append(UP_COMMA);
        requestBuilder.append(query);
        requestBuilder.append(UP_COMMA);
        requestBuilder.append(SPACE_CHARACTER);
    }

    @Override
    public void addNumberParametr(Number number) {
        requestBuilder.append(number.toString());
    }

    @Override
    public String toString() {
        return requestBuilder.toString();
    }

    @Override
    public String prepareQuery() {
        return requestBuilder.toString();
    }

    public static class QueryBuilder {

        private final QueryImpl queryImpl;

        public QueryBuilder(SupportedRequestTypes requestType) {
            queryImpl = new QueryImpl(requestType);
        }

        public QueryBuilder addStringParametr(String string) {
            queryImpl.addStringParametr(string);
            return this;
        }

        public QueryBuilder addNumberParametr(Number number) {
            queryImpl.addNumberParametr(number);
            return this;
        }

        public QueryBuilder addSqlCommand(String sqlCommand) {
            queryImpl.addSqlCommand(sqlCommand);
            return this;
        }

        public QueryBuilder addBooleanParameter(Boolean boolValue) {
            queryImpl.addBooleanParametr(boolValue);
            return this;
        }

        public Query build() {
            queryImpl.addSqlCommand(";");
            return queryImpl;
        }

    }

    public static void main(String[] args) {
        String query = new QueryImpl.QueryBuilder(SupportedRequestTypes.SELECT).addSqlCommand("*").addSqlCommand("FROM")
                .addSqlCommand("db")
                .addSqlCommand("WHERE")
                .addSqlCommand("something=").addNumberParametr(1234).build().prepareQuery();

        System.out.println(query);
    }
}
